package com.banana.springcloud.controller;

import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import com.banana.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @author layman
 * @description: controller
 * @date 2021/1/15
 */
@RestController
@Slf4j
@RequestMapping("/payment")
public class ConsulPaymentController {

    @Autowired
    private PaymentService paymentService;

    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment){
        int result =  paymentService.create(payment);
        if(result > 0){
            return new CommonResult(200,"插入数据成功,serverPort:"+serverPort,payment.getId());
        }
        return new CommonResult(400,"插入数据失败");
    }

    @GetMapping("/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        //@PathVariable:路径变量，是spring3.0的新功能，能够接收请求路径中占位符的值
        log.info("-------获取id的值-------："+id);
        Payment payment =  paymentService.getPaymentById(id);
        if(payment !=null ){
            log.info("****查询结果："+payment);
            return new CommonResult(200,"查询数据成功,serverPort:"+serverPort,payment);
        }
        return new CommonResult(400,"没有对应的记录，查询id为："+id);
    }

    @RequestMapping("/discovery")
    public Object discovery(){
        return this.discoveryClient;
    }

    @RequestMapping(value = "/consulInfo")
    public String paymentConsulInfo() {
        return "springCloud with consul: " + serverPort + "\t" + UUID.randomUUID().toString();
    }
}
